package com.hkbigdata.sybx.controller;


import com.hkbigdata.sybx.bean.UserFeedback;
import com.hkbigdata.sybx.service.UserFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/userFeedback")
public class UserFeedbackController {
    @Autowired
    private UserFeedbackService userFeedbackService;
    @PostMapping("/insert")
    public Map<String, Object> UserFeedbackInsert(@RequestBody UserFeedback userFeedback, @RequestHeader("X-User-Account") String headerUserId) {
        System.out.println("接收到的用户ID: " + userFeedback.getUserId());
        System.out.println("请求头用户ID: " + headerUserId);
        boolean b = userFeedbackService.FeedbackInsert(userFeedback);
        Map<String, Object> response = new HashMap<>();
        response.put("success", b);
        return response;
    }

    /**
     * 删除用户反馈
     */
    @DeleteMapping("/delete/{userId}")
    public Map<String, Object> deleteFeedback(@PathVariable String userId) {
        boolean result = userFeedbackService.deleteFeedbackByUserId(userId);
        Map<String, Object> response = new HashMap<>();
        response.put("success", result);
        response.put("message", result ? "反馈删除成功" : "反馈删除失败");
        return response;
    }

    /**
     * 更新用户反馈
     */
    @PutMapping("/update")
    public Map<String, Object> updateFeedback(@RequestBody UserFeedback userFeedback) {
        boolean result = userFeedbackService.updateFeedback(userFeedback);
        Map<String, Object> response = new HashMap<>();
        response.put("success", result);
        response.put("data", userFeedback);
        response.put("message", result ? "反馈更新成功" : "反馈更新失败");
        return response;
    }

    /**
     * 根据用户ID查询反馈
     */
    @GetMapping("/{userId}")
    public Map<String, Object> getFeedbackByUserId(@PathVariable String userId) {
        List<UserFeedback> feedbackList = userFeedbackService.getFeedbackByUserId(userId);
        Map<String, Object> response = new HashMap<>();
        response.put("success", !feedbackList.isEmpty());
        response.put("data", feedbackList);
        response.put("count", feedbackList.size());
        response.put("message", !feedbackList.isEmpty() ? "查询成功" : "未找到反馈记录");
        return response;
    }

    /**
     * 查询所有反馈
     */
    @GetMapping("/all")
    public Map<String, Object> getAllFeedback() {
        List<UserFeedback> feedbackList = userFeedbackService.getAllFeedback();
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("data", feedbackList);
        response.put("count", feedbackList.size());
        response.put("message", "查询成功");
        return response;
    }
}
